package edu.cmu.cs.stage3.alice.authoringtool.util;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.event.MouseInputListener;

public class AliceMenuItemUI extends javax.swing.plaf.basic.BasicMenuItemUI {

//Rem @Override
 protected MouseInputListener createMouseInputListener( JComponent c ) {
  return new MouseInputHandler();
 }

 protected class MouseInputHandler implements MouseInputListener {
  public void mouseReleased( MouseEvent e ) {
   MenuSelectionManager manager = MenuSelectionManager.defaultManager();
   Point p = e.getPoint();

   if( ( p.x >= 0 ) && ( p.x < menuItem.getWidth() ) && ( p.y >= 0 ) && ( p.y < menuItem.getHeight() ) ) {
    MenuElement[] path = getPath();
    manager.clearSelectedPath();
    menuItem.doClick( 0 );

    // HACK
    //LOB for (MenuElement element : path)
    int t_tam = path!=null ? path.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
     if( path[i_i] instanceof AlicePopupMenu ) { //L if( element instanceof AlicePopupMenu )
      ((AlicePopupMenu)path[i_i]).setVisible( false ); //L ((AlicePopupMenu)element).setVisible( false )
     }
    }
   } else {
    manager.processMouseEvent( e );
   }
  }

  public void mouseEntered( MouseEvent e ) {
   MenuSelectionManager manager = MenuSelectionManager.defaultManager();
   int modifiers = e.getModifiers();

   // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
   if( ( modifiers & ( InputEvent.BUTTON1_MASK | InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK ) ) != 0 ) {
    MenuSelectionManager.defaultManager().processMouseEvent( e );
   } else {
    manager.setSelectedPath( getPath() );
   }
  }

  public void mouseExited( MouseEvent e ) {
   MenuSelectionManager manager = MenuSelectionManager.defaultManager();
   int modifiers = e.getModifiers();

   // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
   if( ( modifiers & ( InputEvent.BUTTON1_MASK | InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK ) ) != 0 ) {
    MenuSelectionManager.defaultManager().processMouseEvent( e );
   } else {
    MenuElement[] path = manager.getSelectedPath();

    if( path.length > 1 ) {
     MenuElement[] newPath = new MenuElement[path.length - 1];
     int i;
     int c;

     for( i = 0, c = path.length - 1; i < c; i++ ) {
      newPath[i] = path[i];
     }

     manager.setSelectedPath( newPath );
    }
   }
  }

  public void mouseDragged( MouseEvent e ) {
   MenuSelectionManager.defaultManager().processMouseEvent( e );
  }

  public void mouseClicked( MouseEvent e ) {}
  public void mousePressed( MouseEvent e ) {}
  public void mouseMoved( MouseEvent e ) {}
 }

 //Rem @Override
 public MenuElement[] getPath() {
  MenuSelectionManager m = MenuSelectionManager.defaultManager();
  MenuElement[] oldPath = m.getSelectedPath();
  MenuElement[] newPath;
  int i = oldPath.length;

  if( i == 0 ) {
   return new MenuElement[0];
  }

  Component parent = menuItem.getParent();

  if( oldPath[i - 1].getComponent() == parent ) {
   newPath = new MenuElement[i + 1];
   System.arraycopy( oldPath, 0, newPath, 0, i );
   newPath[i] = menuItem;
  } else {
   java.util.Vector path = new java.util.Vector();
   MenuElement me = menuItem;
   while( me instanceof MenuElement ) {
    path.add( 0, me );
    if( me instanceof JPopupMenu ) {
     Object o = ((JPopupMenu)me).getInvoker();
     if( (o instanceof MenuElement) && (o != me) ) {
      me = (MenuElement)o;
     } else {
      me = null;
     }
    } else if( me instanceof JMenuItem ) {
     Object o = ((JMenuItem)me).getParent();
     if( (o instanceof MenuElement) && (o != me) ) {
      me = (MenuElement)o;
     } else {
      me = null;
     }
    } else {
     me = null;
    }
   }

   newPath = (MenuElement[])path.toArray( new MenuElement[0] );
  }

  return newPath;
 }

}
